package com.mopa.viewPage;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
public class VflipperLayout extends RelativeLayout{
	private static final int ID_RADIOGROUP_LOC = 11;
	private static final int ID_RADIOBTN_GPRS = 12;
	private static final int ID_RADIOBTN_WIFI = 13;
	private static final int ID_RADIOBTN_GPS = 14;
	private RadioGroup  mRadioGroupLoc;
	private RadioButton mRadioBtnGprs;
	private RadioButton mRadioBtnWifi;
	private RadioButton mRadioBtnGps;
	private ViewLayoutA mViewLayoutA;
	private ViewLayoutB mViewLayoutB;
	private ViewLayoutC mViewLayoutC;
	private ViewLayoutD mViewLayoutD;
	private ViewPager   mViewPager;//ҳ������
	private List<View>  mViewArray; // Tabҳ���б�
	private PageItemAdapter adapter;
	public VflipperLayout(Context context) {
		super(context);
		initLayout(context);
		// TODO Auto-generated constructor stub
	}

	private void initLayout(Context context) {
		// TODO Auto-generated method stub
		Bitmap bmNull=null;
		BitmapDrawable bDrawable=new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.browse_bg_tile));
		bDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		this.setBackgroundDrawable(bDrawable);
		
		LayoutParams relativeParams=new LayoutParams(LayoutParams.FILL_PARENT, 125);
		relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		mRadioGroupLoc=new RadioGroup(context);
		mRadioGroupLoc.setOrientation(RadioGroup.HORIZONTAL);
		mRadioGroupLoc.setId(ID_RADIOGROUP_LOC);
		mRadioGroupLoc.setGravity(Gravity.CENTER_HORIZONTAL);
		this.addView(mRadioGroupLoc, relativeParams);
		
		RadioGroup.LayoutParams redioGroupParams=new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, 105);
		redioGroupParams.weight=1;
		mRadioBtnGprs=new RadioButton(context);
		mRadioBtnGprs.setBackgroundResource(R.drawable.nav_inactive_tile);
		mRadioBtnGprs.setText("GPRS");
		mRadioBtnGprs.getPaint().setFakeBoldText(true);
		mRadioBtnGprs.setButtonDrawable(new BitmapDrawable(bmNull));
		mRadioBtnGprs.setId(ID_RADIOBTN_GPRS);
		mRadioBtnGprs.setTextColor(0xff839359);
		mRadioBtnGprs.setOnClickListener(mOnClickListener);
		mRadioBtnGprs.setGravity(Gravity.CENTER);
		mRadioGroupLoc.addView(mRadioBtnGprs,redioGroupParams);
		
		redioGroupParams=new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, 105);
		ImageView mImgSplitlineLeft=new ImageView(context);
		mImgSplitlineLeft.setImageResource(R.drawable.nav_divider);
		mRadioGroupLoc.addView(mImgSplitlineLeft, redioGroupParams);
		
		redioGroupParams=new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, 105);
		redioGroupParams.weight=1;
		mRadioBtnWifi=new RadioButton(context);
		mRadioBtnWifi.setBackgroundResource(R.drawable.nav_inactive_tile);
		mRadioBtnWifi.setText("WIFI");
		mRadioBtnWifi.setButtonDrawable(new BitmapDrawable(bmNull));
		mRadioBtnWifi.getPaint().setFakeBoldText(true);
		mRadioBtnWifi.setId(ID_RADIOBTN_WIFI);
		mRadioBtnWifi.setTextColor(0xff839359);
		mRadioBtnWifi.setOnClickListener(mOnClickListener);
		mRadioBtnWifi.setGravity(Gravity.CENTER);
		mRadioGroupLoc.addView(mRadioBtnWifi,redioGroupParams);
		mRadioGroupLoc.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				changeCheckStatus(checkedId);
			}
		});
		
		redioGroupParams=new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT,105);
		ImageView mImgSplitlineRight=new ImageView(context);
		mImgSplitlineRight.setImageResource(R.drawable.nav_divider);
		mRadioGroupLoc.addView(mImgSplitlineRight, redioGroupParams);
		
		redioGroupParams=new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, 105);
		redioGroupParams.weight=1;
		mRadioBtnGps=new RadioButton(context);
		mRadioBtnGps.setButtonDrawable(new BitmapDrawable(bmNull));
		mRadioBtnGps.setBackgroundResource(R.drawable.nav_inactive_tile);
		mRadioBtnGps.setText("GPS");
		mRadioBtnGps.setTextColor(0xff839359);
		mRadioBtnGps.getPaint().setFakeBoldText(true);
		mRadioBtnGps.setId(ID_RADIOBTN_GPS);
		mRadioBtnGps.setGravity(Gravity.CENTER);
		mRadioGroupLoc.addView(mRadioBtnGps,redioGroupParams);
		
		mRadioBtnGps.setOnClickListener(mOnClickListener);
		
		relativeParams=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		relativeParams.addRule(RelativeLayout.BELOW,ID_RADIOGROUP_LOC);
		mViewPager=new ViewPager(context);
		this.addView(mViewPager,relativeParams);

		relativeParams=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mViewLayoutA=new ViewLayoutA(context);
		mViewLayoutB=new ViewLayoutB(context);
		mViewLayoutC=new ViewLayoutC(context);
//		mViewLayoutD=new ViewLayoutD(context);
		mViewArray = new ArrayList<View>();
		mViewArray.add(mViewLayoutA);
		mViewArray.add(mViewLayoutB);
		mViewArray.add(mViewLayoutC);
//		mViewArray.add(mViewLayoutD);
		
		
		adapter=new PageItemAdapter(mViewArray);
		mViewPager.setAdapter(adapter);
		
		mViewPager.setOnPageChangeListener(new XonPageChangeListener());
		mViewPager.setCurrentItem(0);
		changeCheckStatus(0);
	}

	private OnClickListener mOnClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case ID_RADIOBTN_GPRS:
				mViewPager.setCurrentItem(0);
				break;
			case ID_RADIOBTN_WIFI:
				mViewPager.setCurrentItem(1);
				break;
			case ID_RADIOBTN_GPS:
				mViewPager.setCurrentItem(2);
				break;
			default:
				break;
			}
		}
	};
	
	private void changeCheckStatus(int checkedId)
	{

		switch (checkedId) {
		case 0:
		case ID_RADIOBTN_GPRS:
			mRadioBtnGprs.setBackgroundResource(R.drawable.nav_active_2options);
			mRadioBtnWifi.setBackgroundResource(R.drawable.nav_inactive_tile);
			mRadioBtnGps.setBackgroundResource(R.drawable.nav_inactive_tile);
			break;
		case 1:
		case ID_RADIOBTN_WIFI:
			mRadioBtnGprs.setBackgroundResource(R.drawable.nav_inactive_tile);
			mRadioBtnWifi.setBackgroundResource(R.drawable.nav_active_2options);
			mRadioBtnGps.setBackgroundResource(R.drawable.nav_inactive_tile);
			break;
		case 2:
		case ID_RADIOBTN_GPS:
			mRadioBtnGprs.setBackgroundResource(R.drawable.nav_inactive_tile);
			mRadioBtnWifi.setBackgroundResource(R.drawable.nav_inactive_tile);
			mRadioBtnGps.setBackgroundResource(R.drawable.nav_active_2options);
			break;
		default:
			break;
		}
	}
    
    /**
     * PageView����:
     */
    private class XonPageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int key) {
			// TODO Auto-generated method stub
				

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			changeCheckStatus(arg0);
		}
    	
    }
    

}
